package sq.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sq.dao.*;
import sq.repository.*;
import sq.service.OrdersService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: sunquan
 * @Date: 2018/3/30
 * @Time: 14:01
 */
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private OrdersRepository ordersRepository;
    @Autowired
    private OrdersItemRepository ordersItemRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AddressRepository addressRepository;
    @Override
    public ArrayList<Cart> findCheckedCart(String checkedId) {
        String[] ids = checkedId.split(",");
        ArrayList<Cart> list =  new ArrayList<Cart>();;
        for (int i = 0; i < ids.length; i++) {
            int id = Integer.parseInt(ids[i]);
            Cart cart=cartRepository.findOne(id);
            list.add(cart);
        }
        return list;
    }

    @Override
    public void ordersAdd(String checkedId,double price,HttpServletRequest request) {
        ArrayList<Cart> list = (ArrayList<Cart>) findCheckedCart(checkedId);
        HttpSession session = request.getSession();
        try {
            String userName = session.getAttribute("userSession").toString();
            User user = userRepository.findUserByUsername(userName);
            Integer userId = user.getUserId();
            Address address = addressRepository.findAddressByUserId(userId);
            Orders orders=new Orders();
            orders.setCreateTime(new Date());
            orders.setPrice(price);
            orders.setStatus(0);
            orders.setUserId(userId);
            orders.setUsername(userName);
            orders.setAddressId(address.getAddressId());
            orders.setAddressDetail(address.getAddressDetail());
            orders.setPhone(user.getPhone());
            ordersRepository.save(orders);
            for (int i = 0; i < list.size(); i++) {
                OrdersItem ordersItem=new OrdersItem();
                ordersItem.setFoodId(list.get(i).getFoodId());
                ordersItem.setBuyNumber(list.get(i).getBuyNumber());
                ordersItem.setFoodName(list.get(i).getFoodName());
                ordersItem.setTotalPrice(list.get(i).getTotalPrice());
                ordersItem.setOrdersId(orders.getOrdersId());
                ordersItem.setCreateTime(new Date());
                ordersItem.setComplete(false);
                ordersItemRepository.save(ordersItem);
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<Orders> findOrders(HttpServletRequest request) {
        HttpSession session = request.getSession();
        try {
            String userName = session.getAttribute("userSession").toString();
            User user = userRepository.findUserByUsername(userName);
            Integer userId = user.getUserId();
            List<Orders> ordersList = ordersRepository.findAllByUserIdOrderByOrdersIdDesc(userId);
            return ordersList;
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<OrdersItem> findOrdersItem(Integer ordersId) {
        List<OrdersItem> ordersItemsList=null;
        ordersItemsList=ordersItemRepository.findAllByOrdersId(ordersId);
        return ordersItemsList;
    }

    @Override
    public List<Orders> findAllOrders() {
        List<Orders> ordersList = ordersRepository.OrderByOrdersIdDesc();
        return ordersList;
    }

    @Override
    public void receive(Integer ordersId) {
        Orders orders=ordersRepository.findOne(ordersId);
        orders.setStatus(1);
        ordersRepository.save(orders);
    }

    @Override
    public void toReceiveStatus(Integer ordersId) {
        Orders orders=ordersRepository.findOne(ordersId);
        orders.setStatus(2);
        ordersRepository.save(orders);
    }

    @Override
    public void complete(Integer ordersId) {
        Orders orders=ordersRepository.findOne(ordersId);
        orders.setStatus(3);
        List<OrdersItem> ordersItemsList=null;
        ordersItemsList=ordersItemRepository.findAllByOrdersId(ordersId);
        for (int i=0;i<ordersItemsList.size();i++){
            ordersItemsList.get(i).setComplete(true);
        }
        ordersRepository.save(orders);
    }

    @Override
    public List<Orders> findFinalOrders() {
        List<Orders> orders= ordersRepository.findAllByStatusOrderByOrdersIdDesc(3);
        return orders;
    }

    @Override
    public List<CountStatistics> findTotalSell(Date begin, Date end) {
        List<CountStatistics> countStatistics= ordersItemRepository.findAllByCompleteAndCreateTimeBetweenGroupByCreateTime(true,begin,end);
        return countStatistics;
    }
    @Override
    public List<CountStatistics> findTop(Date begin, Date end) {
        ArrayList<CountStatistics> countStatistics= (ArrayList<CountStatistics>)ordersItemRepository.findTop(true,begin,end);
        if(countStatistics.size()>10){
            ArrayList<CountStatistics> newList =new ArrayList<CountStatistics>(countStatistics.subList(0, 10));
            return newList;
        }else{
            return countStatistics;
        }

    }

    @Override
    public List<CountStatistics> findqtTop() {
        ArrayList<CountStatistics> countStatistics= (ArrayList<CountStatistics>)ordersItemRepository.findqtTop(true);
        if(countStatistics.size()>10){
            ArrayList<CountStatistics> newList =new ArrayList<CountStatistics>(countStatistics.subList(0, 10));
            return newList;
        }else{
            return countStatistics;
        }
    }
}
